package leetcode.characterstring;

public class getMinKNumsByHeap {
    public static int[] process(int[] arr,int k){
        if (k<1 || k>arr.length){
            return arr;
        }

        int[] kHeap = new int[k];
        for (int i = 0; i!=k ; i++) {
           heapInsert(kHeap,arr[i],i);
        }
        for (int i = 0; i < arr.length; i++) {
            if (arr[i]<kHeap[0]){
                kHeap[0] = arr[i];
                heapify(kHeap,0,kHeap.length);
            }
        }
        return kHeap;
    }

    public static void heapInsert(int[] arr,int value,int index){
        arr[index] = value;

        while (index!=0){
            int parent = (index-1)/2;
            if (arr[parent]<arr[index]){
                swap(arr,parent,index);
                index = parent;
            }else {
                break;
            }
        }

    }

    public static void heapify(int[] arr,int index,int heapSize){
        int left = index*2 +1;
        int right = index*2+2;
        int largest = index;
        while (left<heapSize){
            if (arr[left]>arr[index]){
                largest = left;
            }
            if (right<heapSize && arr[right]>arr[largest]){
                largest = right;
            }
            if (largest != index){
                swap(arr, index, largest);
            }else {
                break;
            }
            index = largest;
            left = index*2+1;
            right = index*2+2;
        }
    }

    public static void swap(int[] arr,int index1,int index2){
        int temp = arr[index1];
        arr[index1] = arr[index2];
        arr[index2] = temp;
    }
}
